package com.niuxiaofa.newlaw.Activity;

import android.util.Log;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.androidkun.xtablayout.XTabLayout;
import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Base.RefreshInterface;
import com.niuxiaofa.newlaw.Bean.CourseLabelListBean;
import com.niuxiaofa.newlaw.Bean.QueryVideoListByLableBean;
import com.niuxiaofa.newlaw.Network.ApiService;
import com.niuxiaofa.newlaw.Network.LawUniversalPresenter;
import com.niuxiaofa.newlaw.Network.ServiceManager;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.RecyclerViewHelp;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.adapter.CourseAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 课程专区
 */
public class CourseActivity extends BaseActivity implements RefreshInterface.Refresh, LawUniversalPresenter.interface_queryVideoListByLable {



    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;


    @BindView(R.id.tablayout_holder)
    XTabLayout holderTabLayout;

    CourseAdapter courseAdapter;

    String id;

    int currentIndex = 1;



    List<QueryVideoListByLableBean.DataDTO.RecordsDTO> newsList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_course;
    }

    @Override
    protected void initData() {
        setTitle("全部");

        refreshinterface=new RefreshInterface(this);
        courseAdapter = new CourseAdapter(newsList, new CourseAdapter.Onclick() {
            @Override
            public void onclick(int c) {

                String [][] a=new String[][]{{"id",newsList.get(c).getId()+""}};
                ToActivityUtil.toNextActivity(CourseDetailsActivity.class,a);


            }
        });

        RecyclerViewHelp.setRecycler(recyclerview, 0).setAdapter(courseAdapter);

        Call<CourseLabelListBean> call = ServiceManager.getInstance().creat(ApiService.class).courseLabelList();

        call.enqueue(new Callback<CourseLabelListBean>() {
            @Override
            public void onResponse(Call<CourseLabelListBean> call, Response<CourseLabelListBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    CourseLabelListBean baseBean = (CourseLabelListBean) response.body();
                    for (int i = 0; i < baseBean.getData().size(); i++) {
                        holderTabLayout.addTab(holderTabLayout.newTab().setText(baseBean.getData().get(i).getCourseLabelName()));
                    }

                    id = baseBean.getData().get(0).getId() + "";
                    LawUniversalPresenter.queryVideoListByLable(id, currentIndex, CourseActivity.this);
                    //实际的tablayout的点击切换

                    holderTabLayout.addOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(XTabLayout.Tab tab) {
                            int pos = tab.getPosition();
                            id = baseBean.getData().get(pos).getId() + "";
                            currentIndex = 1;

                            setTitle(holderTabLayout.getTabAt(pos).getText().toString());

                            LawUniversalPresenter.queryVideoListByLable(id, currentIndex, CourseActivity.this);


                        }

                        @Override
                        public void onTabUnselected(XTabLayout.Tab tab) {

                        }

                        @Override
                        public void onTabReselected(XTabLayout.Tab tab) {

                        }
                    });


                    setTitle(holderTabLayout.getTabAt(0).getText().toString());
                    holderTabLayout.getTabAt(0).select();
                }
            }

            @Override
            public void onFailure(Call<CourseLabelListBean> call, Throwable t) {
             Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
            }
        });


    }


    @Override
    public void getResponse(QueryVideoListByLableBean u) {

        if (u.getCode() == 1) {
            if (currentIndex==1){
                newsList.clear();
            }
            if (refreshLayout!=null){
                refreshLayout.finishLoadMore();
                refreshLayout.finishRefresh();
            }

            newsList.addAll(u.getData().getRecords());
            courseAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void refresh() {
        currentIndex=1;
        LawUniversalPresenter.queryVideoListByLable(id, currentIndex, CourseActivity.this);

    }

    @Override
    public void loadmore() {
        currentIndex++;
        LawUniversalPresenter.queryVideoListByLable(id, currentIndex, CourseActivity.this);
    }
}
